package com.yaj.mesif.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yaj.mesif.dto.TiOrderDto;
import com.yaj.mesif.entity.TiItem;
import com.yaj.mesif.entity.TiMfgst;
import com.yaj.mesif.entity.TiOrder;
import com.yaj.mesif.mapper.TiOrderMapper;
import com.yaj.mesif.service.TiItemService;
import com.yaj.mesif.service.TiMfgstService;
import com.yaj.mesif.service.TiOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.mesif.unil.RespBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wei
 * @since 2021-08-18
 */
@Service
public class TiOrderServiceImpl extends ServiceImpl<TiOrderMapper, TiOrder> implements TiOrderService {
    @Resource
    private TiOrderMapper tiOrderMapper;
    @Resource
    private TiItemService tiItemService;
    @Resource
    private TiMfgstService tiMfgstService;

    @Value("${sdl.organization_code}")
    private String partyId;

    @Override
    public boolean RegTiOrder(TiOrderDto tiOrderDto) {
        String description ="";
        String bondFlag = "";
        TiOrder tiOrder = new TiOrder();
        BeanUtil.copyProperties(tiOrderDto,tiOrder);

        //设置订单发布日期为当前执行时间
        tiOrder.setReleasedate(DateUtil.date());
        //在库组织
        tiOrder.setPartyid(partyId);
        //組立品目名
        TiItem tiItemOne = tiItemService.getOne(new QueryWrapper<TiItem>().lambda().select(TiItem::getDescription).eq(TiItem::getSegment1, tiOrder.getItemid()));
        if(ObjectUtil.isNotNull(tiItemOne)){
            description = tiItemOne.getDescription();
        }
        tiOrder.setItemname(description);

        //保税フラグ
        TiMfgst tiMfgstOne = tiMfgstService.getOne(new QueryWrapper<TiMfgst>().lambda().select(TiMfgst::getBondFlag).eq(TiMfgst::getMfgstId, tiOrderDto.getMfgstid()));
        if(ObjectUtil.isNotNull(tiMfgstOne)){
            if(bondFlag.equals("B")){
                tiOrder.setBoundflag("1");
            }else if(bondFlag.equals("T")){
                tiOrder.setBoundflag("0");
            }
        }

        int result = tiOrderMapper.insert(tiOrder);
        if(result == 1){
            return true;
        }

        return false;
    }
}
